package org.anrc.poi;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;

public class PoiPreferences extends Activity {
	CheckBox autoUpdate;
	TextView updateUrl;
	Spinner updateFreqSpinner;
	public static final String USER_PREFERENCES = "USER_PREFERENCES";
	public static final String PREF_UPDATE_URL = "PREF_UPDATE_URL";
	public static final String PREF_AUTO_UPDATE = "PREF_AUTO_UPDATE";
	public static final String PREF_UPDATE_FREQ = "PREF_UPDATE_FREQ";

	SharedPreferences prefs;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.poi_preferences);
		updateFreqSpinner = (Spinner) findViewById(R.id.spinner_update_freq);
		autoUpdate = (CheckBox) findViewById(R.id.checkbox_auto_update);
		updateUrl = (TextView) findViewById(R.id.auto_update_url_val);
		populateSpinners();

		prefs = getSharedPreferences(USER_PREFERENCES, Activity.MODE_PRIVATE);
		updateUIFromPreferences();

		Button okButton = (Button) findViewById(R.id.okButton);
		okButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				savePreferences();
				PoiPreferences.this.setResult(RESULT_OK);
				finish();
			}
		});
		Button cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				PoiPreferences.this.setResult(RESULT_CANCELED);
				finish();
			}
		});
	}

	private void savePreferences() {
		int updateIndex = updateFreqSpinner.getSelectedItemPosition();
		boolean autoUpdateChecked = autoUpdate.isChecked();
		String updateUrlStr = updateUrl.getText().toString();
		Editor editor = prefs.edit();
		editor.putBoolean(PREF_AUTO_UPDATE, autoUpdateChecked);
		editor.putInt(PREF_UPDATE_FREQ, updateIndex);
		editor.putString(PREF_UPDATE_URL, updateUrlStr);

		editor.commit();
	}

	private void updateUIFromPreferences() {
		boolean autoUpChecked = prefs.getBoolean(PREF_AUTO_UPDATE, false);
		int updateFreqIndex = prefs.getInt(PREF_UPDATE_FREQ, 2);
		String updateUrlStr = prefs.getString(PREF_UPDATE_URL, "http://poi-radar.googlecode.com/files/trasa_do_srody.kml");
		updateFreqSpinner.setSelection(updateFreqIndex);
		autoUpdate.setChecked(autoUpChecked);
		updateUrl.setText(updateUrlStr);
	}

	private void populateSpinners() {
		// Populate the update frequency spinner
		ArrayAdapter<CharSequence> fAdapter;
		fAdapter = ArrayAdapter.createFromResource(this,
				R.array.update_freq_options,
				android.R.layout.simple_spinner_item);
		fAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		updateFreqSpinner.setAdapter(fAdapter);

	}
}
